package com.aliyun.oss.controller;

import ch.qos.logback.classic.spi.EventArgUtil;
import com.aliyun.oss.pojo.DTO.UserDTO;
import com.aliyun.oss.pojo.Vo.ResultVO;
import com.aliyun.oss.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

// 正确写法
@RestController
//@RequestMapping("/login")
public class LoginController {
    @Autowired
    LoginService loginService;

    @PostMapping("/login")
    public ResultVO userLogin(@RequestBody UserDTO userDTO) {
        String token = loginService.userLogin(userDTO);
        if (token != null) {
//            这里也把用户信息封装给前端
            UserDTO userDTO1 = loginService.selectUserByUsernameAndPassword(userDTO);
            HashMap<String, String> map = new HashMap<>();
            map.put("token", token);
//            将userDTO1也返回给前端
            map.put("username", userDTO1.getUsername());
            map.put("password", userDTO1.getPassword());
            map.put("status",userDTO1.getStatus().toString());
            return ResultVO.success(map);
        } else {
            return ResultVO.error("用户名或密码错误");
        }
    }
}
